﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UcAsp.Models;
namespace UcAsp.WorkFrame
{
    public class AdvPositions
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public static List<Models.AdvPositions> GetListAdvPositions
        {
            get
            {
                List<Models.AdvPositions> list = new List<Models.AdvPositions>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\Content\\config\\AdvPositions.xml");
                string xmlpath = ("root/AdvPositions");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {

                        Models.AdvPositions model = new Models.AdvPositions();

                        foreach (XmlNode xn in xns)
                        {

                            Parser.XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                return list;
            }
        }
        public static Models.AdvPositions GetAdvPositions(string advname)
        {
            Models.AdvPositions adv = GetListAdvPositions.FirstOrDefault(o => o.AdvPositionName.ToUpper() == advname.ToUpper());
            return adv;
        }
        

        public static bool SaveAdvPositions(Models.AdvPositions model)
        {
            XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(path + "\\Content\\config\\AdvPositions.xml");
            string xmlpath = ("root/AdvPositions");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {


                if (xns["AdvPositionName"].InnerText == model.AdvPositionName)
                {
                    xns["AdvChinaName"].InnerText = model.AdvChinaName;
                    xns["AdvHtml"].InnerText = model.AdvHtml.Replace("../","/");
                }
            }
            xmlDoc.Save(path + "\\Content\\config\\AdvPositions.xml");
            return true;
        }
    }
}
